package com.android.aishangauction.fragment.detail;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.view.dialog.MainMsg;
import com.android.aishangauction.R;
import com.android.aishangauction.model.detail.StoreInfo;
import com.android.aishangauction.model.detail.StoreInfoCallback;
import com.android.aishangauction.utils.ConstantsUrl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 店铺详情
 */
public class Fragment_Store_Info extends Fragment {


    @BindView(R.id.img_qrcode)
    ImageView imgQrcode;

    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.info_img)
    LinearLayout info_img;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;

    private FragmentActivity mcontext;
    private View view;


    private LayoutInflater inflater;

    private boolean networkConnected;
    ArrayList<String> listdata = new ArrayList<>();
    private String qrcode;
    private String name;


    public Fragment_Store_Info() {

    }

    private static final String ARG_PARAM1 = "param1";
    private String mParam1;


    public static Fragment_Store_Info newInstance(String param1) {
        Fragment_Store_Info fragment = new Fragment_Store_Info();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_store_layout_info, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        GetDataListData(mParam1);
        return view;
    }


    //获取列表数据
    private void GetDataListData(String id) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERCOLLECTSTOREINFO + "?bid=" + id;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new StoreInfoCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(StoreInfo response, int id) {
                    listdata.clear();
                    if (response.getCode() == 0) {
                        if (response.getData() != null) {
                            tvNote.setText(response.getData().getBusiness().getNote());
                            tvCity.setText(response.getData().getBusiness().getZone());
                            qrcode = response.getData().getBusiness().getQrcode();
                            name = response.getData().getBusiness().getName();
                            ObjectUtils.photo(mcontext, qrcode, imgQrcode);
                            String strTime = ObjectUtils.getStrTime(response.getData().getBusiness().getC_time(), "yyyy-MM-dd");
                            tvTime.setText(strTime);
                            if (response.getData().getBusiness().getBrand() != null) {
                                List<String> brand = response.getData().getBusiness().getBrand();
                                listdata.addAll(brand);
                                initTab();
                            }
                            progress.setVisibility(View.GONE);

                        }
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }

                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }

    /**
     * 热门活动
     */
    private void initTab() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels / 3, LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < listdata.size(); i++) {
            LinearLayout item_home_grid = (LinearLayout) LayoutInflater.from(mcontext).inflate(R.layout.store_logo_grid, null);
            item_home_grid.setLayoutParams(params);
            ImageView imglogo = (ImageView) item_home_grid.findViewById(R.id.imglogo);
            ObjectUtils.photo(getActivity(), listdata.get(i), imglogo);

            info_img.addView(item_home_grid);
        }
    }


    @OnClick({R.id.ll_qrcode, R.id.click_resetnetwork_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_qrcode:
                MainMsg mainMsg = new MainMsg(mcontext);
                mainMsg.show(mcontext.getWindow().getDecorView(), 4, name, qrcode);

                break;
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                GetDataListData(mParam1);
                break;
        }
    }
}
